package com.hbyj.struts.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.YjdcYjdc06;

public class YjdcKbServletFor3G extends HttpServlet {
	
	
	private String uploadPath; // 上传文件的目录
    private String tempPath; // 临时文件目录
    File tempPathFile;
    
    public void init() throws ServletException {
		String serverWordPath = "uploadfolder/simple/yjdc/document";
		uploadPath = getServletContext().getRealPath("/"+serverWordPath);
		tempPath = uploadPath+"\\buffer\\";
        File uploadFile = new File(uploadPath);
        if (!uploadFile.exists()) {
            uploadFile.mkdirs();
        }
        File tempPathFile = new File(tempPath);
         if (!tempPathFile.exists()) {
            tempPathFile.mkdirs();
        }
     }


 public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
 {
   try 
   {
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // Set factory constraints
    factory.setSizeThreshold(40960000); // 设置缓冲区大小，这里是4kb
    factory.setRepository(tempPathFile);// 设置缓冲区目录
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Set overall request size constraint
    upload.setSizeMax(419430400); // 设置最大文件尺寸，这里是40MB
    List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
    Iterator<FileItem> i = items.iterator();
    while (i.hasNext())
    {
     FileItem fi = (FileItem) i.next();
     String fileName = fi.getName();
     if (fileName != null)
     {
      File fullFile = new File(fi.getName());
      File savedFile = new File(uploadPath, fullFile.getName());
      fi.write(savedFile);
     }
     //此时往数据库里面插入一条，fileName，通过这来找时间和统一编号，然后插入数据库
     String yjdc06000 = fileName.split("-")[0];
     String jbqk0102 = fileName.split("-")[1];
     String timeStr = fileName.split("-")[2];
     SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMddHHmmss");
     Date yjdc06080 = formatDate.parse(timeStr);
     //将时间转换成date
     YjdcYjdc06 yjdc06 = new YjdcYjdc06();
	 yjdc06.setYjdc06000(yjdc06000);
	 yjdc06.setJbqk0102(jbqk0102);
	// yjdc06.setYjdc06010("02");
	 //快报
	 String hql = "from YjdcYjdc06 where yjdc06010 like 'YK____' order by yjdc06010 desc";
	 List list = DbTools.findByHql(hql);
	 if(list.size()>0){
		 YjdcYjdc06 yjdc06Biggest = (YjdcYjdc06)list.get(list.size()-1); 
		 String biggest = yjdc06Biggest.getYjdc06010();
		 String num = biggest.split("YK")[1];
		 int newCodeNum = Integer.valueOf(num)+1;
		 String newCode = "";
		 if(newCodeNum<10){
			 newCode = "000"+newCodeNum;
		 }else if(newCodeNum>=10&&newCodeNum<100){
			 newCode = "00"+newCodeNum;
		 }else if(newCodeNum>=100&&newCodeNum<1000){
			 newCode = "0"+newCodeNum;
		 }else{
			 newCode = newCodeNum+"";
		 }
		 yjdc06.setYjdc06010("YK"+newCode);
	 }else{
		 yjdc06.setYjdc06010("YK0001");
	 }
	 yjdc06.setYjdc06080(yjdc06080);
	 try {
		 DbTools.saveObj(yjdc06);
	} catch (Exception e) {
		System.out.println(e.getMessage());
	    // 可以跳转出错页面
	    e.printStackTrace();
	}
    }
    System.out.print("upload succeed");
   }
   catch (Exception e)
   {
    System.out.println(e.getMessage());
    // 可以跳转出错页面
    e.printStackTrace();
   }
 }




}
